<template>
  <j-modal
    title="关键词配置"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="form" :rules="validatorRules">
        <a-form-model-item label="关键词" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
          <a-input v-model="form.name" placeholder="请输入关键词" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { keywordAdd, keywordEdit } from '@/api/information.js'
export default {
  name: 'HandleModal',
  components: {},
  data() {
    return {
      labelCol: {
        xs: { span: 10 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 10 },
        sm: { span: 16 },
      },
      width: 600,
      visible: false,
      form: {
        name: '',
      },
      disableSubmit: false,
      confirmLoading: false,
      isAdd: true,
      validatorRules: {
        name: [{ required: true, message: '请输入关键词!' }],
      },
    }
  },
  async created() {},
  methods: {
    show(record) {
      if (record) {
        this.form.id = record.id
        this.form.name = record.name
        this.isAdd = false
      } else {
        this.form.name = ''
        this.form.id = ''
        this.isAdd = true
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          if (this.isAdd) {
            keywordAdd(this.form)
              .then((res) => {
                if (res.success) {
                  this.$message.success(res.message)
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.message)
                }
              })
              .catch((e) => {
                this.$message.warn('请求失败！')
              })
              .finally(() => {
                this.confirmLoading = false
                this.close()
              })
          } else {
            keywordEdit(this.form)
              .then((res) => {
                if (res.success) {
                  this.$message.success(res.message)
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.message)
                }
              })
              .catch((e) => {
                this.$message.warn('请求失败！')
              })
              .finally(() => {
                this.confirmLoading = false
                this.close()
              })
          }
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style lang="less" scoped></style>
